<?
	class InputLookUp
	{
		var $cField;
		var $cFieldActions = '';
		var $cFile;
		var $oValue;
		var $oDescription;
		var $oForm;
		var $cFormName;
		var $bHidden;
		var $aActions;
		var $cWidth = "252";
		var $cStyle;
		var $cParameter;
		var $cUnits;

  function getUnits()
	{
	  return $this->cUnits;
	}
  function setUnits( $cUnits )
	{
	  $this->cUnits = $cUnits;
	}

function setEmptyText( $cEmptyText )
	{
		$this->oDescription->setEmptyText( $cEmptyText );
	}

function getValueAsString ()
	{
		return $this->getValue ();
	}
function setValueFromString ( $cValue )
	{
		$this->setValue ( $cValue );
	}
  function setParameter ( $cParameter )
  {
		$this->cParameter = $cParameter;
  }
  function setHelp ( $cHelp )
	{
		$this->oDescription->setHelp ( $cHelp );
		//$this->oDescription->aEvents['onfocus'] .= "showContextHelp(this);";
		//$this->oDescription->aEvents['onblur'] .= "hideContextHelp(this);";
	}
	function getHelp()
	{
		return $this->oDescription->getHelp();
	}
  function getLabel()
	{
	  return $this->oDescription->getLabel();
	}
  function setStyle( $cStyle )
	{
		$this->oDescription->setStyle( $cStyle );
	}

	function getField()
	{
		return $this->cField;
	}
	function setField( $cField)
	{
		$this->cField = $cField;
		$this->oValue->setField( $cField . "_Key");
		$this->oDescription->setField( $cField . "_Desc");
	}
	function setFieldActions( $cField)
	{
		$this->cFieldActions = $cField;
	}
	function getFieldActions()
	{
		return ( $this->cFieldActions  == '' ) ? $this->cField : $this->cFieldActions ;
	}

	function setWidth( $cWidth )
	{
		$this->cWidth = $cWidth;
		$this->oDescription->setWidth( $cWidth ) ;
	}

	function setDefault( $cValue )
	{
		if ( $this->oValue->isEmpty() ) $this->oValue->setValue ( $cValue );
	}
	function addAction ( $nModo, $cAction , $cIcon = '', $cNombre = '')
		{
			if ( $cAction )
			{
				$this->aActions[] = array ( $cAction, $cIcon , $cNombre, $nModo);
			}
		}
	// $cParametros: Son parametros fijos que se los agrega a la accion
	// $cField: Es optativo, se ingresa el nombre de un campo del formulario, y antes de abrir la accion, le pasa el valor que tiene.
	function addActionArray ( $nModo, $aAction , $cParametros = '', $cField = '')
		{
			if ( is_array($aAction) )
			{
				// Agrega a la Url los parametros configurados por Objeto(InputLookUp->setParameter) y por Accion (addActionArray)
				if ( $cParametros ) $aAction[0] .=  '&' . $cParametros;
				if ( $this->cParameter ) $aAction[0] .=  '&' . $this->cParameter;
				$aAction[] = $nModo;
				$aAction[] = $cField;
				$this->aActions[] = $aAction;
			}
		}
	// Precondiciones:
	// 		1. Tiene que estar seteado el Form o FormName
  function setOnChangeAutoSubmit( &$oForm )
	{
		if ( $oForm ) $this->setForm($oForm);
		if ( !$this->oForm) addMessage('No esta seteado el Form');
	  if ( MODO_AJAX == true )
	  {
		$cUrl = $this->oForm->getUrlAutoRefresh();
		$this->setOnChange( " return sendForm(\"$cUrl\", \"" . $this->getFormName() . "\",refreshElement, \"" . strtolower(get_class($this->oForm)) . "\", true, false  );"  );
	  } else
	  {
		$this->setOnChange( $this->getFormName() . ".action=\"#\";" . $this->getFormName() . ".submit();"  );
	  }
	}		
  function setOnChangeSubmit( $bOn )
	{
	  $this->setOnChange( $this->getFormName() .  '.submit();' );
	}
  function setForm( &$oForm )
	{
	  $this->oForm =& $oForm;
	}
  function setFormName( $cFormName )
	{
		$this->cFormName = $cFormName;
	}
  function getFormName()
	{
		// Si esta seteado el valor => lo devuelve
		if ( $this->cFormName ) return $this->cFormName ;
		// Sino se fija si tiene el form
		if ( $this->oForm ) return $this->cFormName  = $this->oForm->cFormName;
		//  Y por ultimo se fija si viene por Parametro 
		$cFormName = getParam( 'cFormName' );
		if ( $cFormName ) 
		{
			$this->cFormName = $cFormName; 
			return $this->cFormName ;
		}
		addMessage('No esta seteado el Form');
		return false;
	}
	function showActions()
		{
			$cReturn = '';
			if ( $this->bHidden || !is_array($this->aActions) ) return "";

			foreach ( $this->aActions as $aAction )
			{
				$nModo    = $aAction[3];
				$cIcono    = $aAction[1];
				$cNombre  = $aAction[2];
				$cField  = $aAction[4];
				$cUrl     = replaceString( PARAM_REFRESH, 'action', $aAction[0] );
				$cUrl     = replaceString( PARAM_MODE, DIV_POPUP, $cUrl );
				$cUrl = replaceString( PARAM_TARGET, "div_popup" . (getPopupNumber() + 1) . "_inside", $cUrl);
				$cHRef = "href='javascript: void(0);' onclick=\"javascript: openLookUp ( '$cUrl' , '" . $this->getFieldActions() . "', '" . $this->getFormName() . "', '" . $this->cField . "_Key', '" . $this->cField . "_Desc' , " . $nModo . ", '$cField' );return false; \" ";
				$cReturn .=  "&nbsp;<a $cHRef ><img src='$cIcono' align='top' border=0 width='16' height='16' alt='$cNombre' title='$cNombre' /></a>";
			}
			return $cReturn;
		}
	  function isEmpty ()
		{
		  return (empty($this->cValue) || $this->cValue == $this->cEmptyText) ? true: false;
		}
	function setOnChange ( $cEvent )
		{
			$this->oValue->setOnChange ( $cEvent );
		}
	function setHidden ( $bOn = true )
		{
			$this->oDescription->setHidden( $bOn );
			$this->bHidden = $bOn;
		}
	function setValue( $cValue )
		{
			$this->oValue->setValue ( $cValue );
		}
	function getDescription ( )
		{
			return $this->oDescription->getValue();
		}
	function setDescription( $cValue )
		{
			$this->oDescription->setValue ( $cValue );
		}
	function showLabel( $cDescription = '', $cAccessKey = '' )
		{
			return $this->oDescription->showLabel( $cDescription , $cAccessKey );
		}
	function setMandatory( $bMandatory = true)
	{
		//$this->oValue->setMandatory( $bMandatory );
		$this->oDescription->setMandatory( $bMandatory );
	}
	function setLabel( $cDescription , $cAccessKey  )
		{
			$this->oDescription->setLabel( $cDescription , $cAccessKey );
		}
	function setInputText ( $bOn = true )
	{
		if ( $bOn )
		{
			$this->oDescription->setDisabled( false);
		}
	}
	function setDisabled ( $bDisabled )
	{
		$this->oDescription->setDisabled( $bDisabled );
	}
	function setEditable ( $cUrl  )
	{
		$this->oDescription->setDisabled( false);
		$this->oDescription->setOnKeyPress ( " return keyPressLookUp( event  ); ");
		$cEvent = "changeLookUp( \"" . $this->cField . "\", \"$cUrl\", event , \"" . $this->cField . "_Desc\", \"delaySendLoopkUP\" );";
		$this->oDescription->setOnKeyUp ( $cEvent );
		$cEvent = "return blurLookUp( \"" . $this->cField . "\", \"$cUrl\", event, \"" . $this->oDescription->cLabel . "\" , " . ($this->oDescription->bMandatory ? 1:0) .  " );";
		$this->oDescription->setOnBlur ( $cEvent );
		$cEvent = "return keydownLookUp( \"" . $this->cField . "\", \"$cUrl\", event, \"" . $this->cField . "_Desc\" );";
		$this->oDescription->setOnKeyDown( $cEvent );
	}

	function InputLookUp( $cLabel, $cField, $cValue = '', $bMandatory = false, $cValueDescription = '' )
		{
			$this->cField = $cField;
		// Crea los campos de edicion
			$this->oValue       = new InputText ( '', $this->cField . '_Key' , $cValue );
			$this->oDescription = new InputText ( $cLabel, $this->cField . '_Desc', $cValueDescription );
			$this->oDescription->setLength(120);
			$this->oDescription->setDisabled();
			$this->setHelp('Este es un campo de tipo busqueda, puede escribir en el mismo para seleccionar el registro o usar el buscador, o bien agregar uno nuevo');
		// Configura los campos de edicion
			$this->oValue->setHidden();
			$this->oDescription->setMandatory( $bMandatory );
		// Si viene por GET el Value lo setea
			if ( getParam($cField) )
				{
					$this->oValue->setValue( getParam($cField) );
				}
		}
	function make()
		{
			$cReturn = '';
			if ( $this->bHidden )
			{
				$cReturn = $this->oValue->make();
			} else
			{
				// Arma y devuelve el control
					$cReturn .= "<div style='width: " . $this->cWidth . "px;' class='divLookup' id='divLookUp_" . $this->cField . "'></div>";
					$cReturn .= $this->oValue->make();
					$cReturn .= $this->oDescription->make();
			}
			return $cReturn;
		}
	  function getValue()
		{
		  return $this->oValue->getValue() == $this->cEmptyText ? '': $this->oValue->getValue();
		}
	}
?>
